/**
 * Created by WUJO5 on 2/23/2018.
 */
var reportDetail = require('../models/reportDetail').reportDetail;
var reportSummary = require('../models/reportSummary').reportSummary;
var moment = require('moment');

var getSummaryById = function (data,success,error) {
    reportSummary.findOne({automationId:data.automationId},function (err,result) {
        if(err){
            error({status:'400',reason:err})
        } else {
            success(result);
        }
    })
};

var getDetailById = function (data,success,error) {
    reportDetail.find({automationId:data.automationId},function (err,result) {
        if(err) {
            error({status:'400',reason:err})
        } else {
            success(result);
        }
    })
};

var getDetailBytestCaseName = function (data,success,error) {
    reportDetail.find({automationId:data.automationId,testCaseName:data.testCaseName},function(err,result){
        if(err){
            error({status:'400',reason:err})
        } else {
            success(result);
        }
    })
}

function addReportSummary(req, res, next){
    var reports = req.body;
    reports.createdDate = moment(new Date()).format('DD MMM YYYY HH:mm');
    new reportSummary(reports).save(function(err, data){
      if(err){
        next(err);
      }else{
        res.send({status:'200',data:req.body.automationId});
      }
    });
}
  
function addReportDetail(req, res, next){
    var reportd = req.body;
    // console.log(JSON.stringify(reportd))
    reportd.createdDate = moment(new Date()).format('DD MMM YYYY HH:mm');
    new reportDetail(reportd).save(function(err, data){
      if(err){
        next(err);
      }else{
        res.send({status:'200'});
      }
    });
}


module.exports = {
    getSummaryById:getSummaryById,
    getDetailById:getDetailById,
    getDetailBytestCaseName:getDetailBytestCaseName,
    addReportSummary: addReportSummary,
    addReportDetail: addReportDetail 

}